package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CodeEnum;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;


import javax.servlet.http.HttpServletRequest;


@Service
@Slf4j
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    EmployeeMapper employeeMapper;



    public R<Page<Employee>> pageEmployee(int page, int pageSize, String name) {
        //构造分页构造器
        Page<Employee> pageInfo = new Page<>(page, pageSize);
        //构造条件构造器
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        //过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(name), "name", name);
        //排序条件
        queryWrapper.orderByDesc("update_time");
        //执行查询
        employeeMapper.selectPage(pageInfo, queryWrapper);
        return R.success(pageInfo);
    }

    @Override
    public void loginOut(HttpServletRequest request) {
        request.getSession().removeAttribute("employee");
    }

    @Override
    public R<Employee> login(HttpServletRequest request, Employee employee) {
        String password = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());

        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", employee.getUsername());
        Employee emp = employeeMapper.selectOne(queryWrapper);

        if (emp == null) {
            return R.error("登陆失败");
        }

        if (!emp.getPassword().equals(password)) {
            return R.error("登陆失败");
        }

        if (emp.getStatus() == 0) {
            return R.error("账号已禁用");
        }

        request.getSession().setAttribute("employee", emp.getId());
        return R.success(emp);
    }

    @Override
    public R<String> saveEmployee(HttpServletRequest request, Employee employee) {
        Employee hasEmp = employeeMapper.selectByUsername(employee.getUsername());
        if (hasEmp != null) {
            return R.error("用户已存在");
        }

        String password = DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(password);
//        //获取当前时间并存入
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());

//        //获取页面登陆的用户并且进行用户增加，并将修改人注入
        Long creatId = (Long) request.getSession().getAttribute("employee");
//        employee.setCreateUser(creatId);
//        employee.setUpdateUser(creatId);

        employeeMapper.insert(employee);
        return R.success("保存成功");
    }

    @Override
    public int changeStatus(Employee httpEmployee, HttpServletRequest request) {
        Employee employee = employeeMapper.selectById(httpEmployee.getId());
        if (employee == null) {
            return CodeEnum.ENTITY_IS_DELETE;
        }
        if (employee.getId() == 1 && employee.getStatus() == 0) {
            return CodeEnum.ENTITY_IS_ADMIN;
        }
//        Long updateUser = (Long) request.getSession().getAttribute("employee");
//        httpEmployee.setUpdateTime(LocalDateTime.now());
//        httpEmployee.setUpdateUser(updateUser);
        employeeMapper.updateById(httpEmployee);
        return CodeEnum.SUCCESS;
    }

    @Override
    public Employee getEmpById(Long id) {
        return employeeMapper.selectById(id);
    }
}
